package weibo.tools;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import weibo.DecodeWeiboCaptcha;

public class FLTest {
	
	public static void main(String[] args) throws Exception {
		String name = "/Users/hlian/Desktop/sample2.gif";
		BufferedImage img = ImageIO.read(new File(name));
		for (int y = 0; y < img.getHeight(); y++) {
			for (int x = 0; x < img.getWidth(); x++){
				if(isBackGround(img.getRGB(x, y))){
					img.setRGB(x, y, Color.white.getRGB());
				}
			}
		}
//		ImageIO.write(img, "png", new File("/Users/hlian/Desktop/test2.png"));
		int start = 0;
		for(int y=1;y<100;y++){
			Rectangle rectangle = DecodeWeiboCaptcha.findLetterRectangle(img,start);
			start = rectangle.x+rectangle.width+1;
			if(rectangle.height ==0 || rectangle.width == 0){
				continue;
			}
			BufferedImage subCode = img.getSubimage(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
			ImageIO.write(subCode, "png", new File("/Users/hlian/Desktop/almost"+y+".png"));
		}
		
	}
	
	private static boolean isBackGround(int rgb){
		Color c = new Color(rgb);
		if(c.getRed()>100 && c.getGreen()> 100 && c.getBlue() > 100){
			return true;
		} else{
			return false;
		}
	}

}
